package com.keepers.repair.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.keepers.repair.R;
import com.keepers.repair.adapter.RepairAdapter;
import com.keepers.repair.helper.IntentHelper;
import com.keepers.repair.helper.SpHelper;
import com.keepers.repair.model.RepairModel;
import com.keepers.repair.model.UserModel;
import com.keepers.repair.net.DataLoader;
import com.keepers.repair.utils.AlertToast;
import com.keepers.repair.view.PullRecyclerView;
import com.keepers.repair.view.TitleBar;

/**
 * auth aboom
 * date 2018/11/18
 */
public class MyMaintenanceActivity extends BaseActivity {

    private PullRecyclerView recyclerView;
    private UserModel        loginUserInfo;
    private RepairAdapter    repairAdapter;
    private TextView         emptyView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maintenance);
        TitleBar titleBar = f(R.id.title_bar);
        titleBar.initializeHeader("我的任务");
        emptyView = f(R.id.empty_view);
        loginUserInfo = SpHelper.getLoginUserInfo();
        recyclerView = f(R.id.list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setOnRefreshListener(this::reload);
        repairAdapter = new RepairAdapter(R.layout.item_repair);
        repairAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            RepairModel item = repairAdapter.getItem(position);
            if (item != null) {
                IntentHelper.goSend(this, item.Gid);
            }
        });
        recyclerView.setAdapter(repairAdapter);
        reload();
    }

    private void reload() {
        recyclerView.showRefresh();
        rxHelper.pend(DataLoader.getRepairList(loginUserInfo.Gid, loginUserInfo.GroupGid, o -> {
            recyclerView.loadComplete(true);
            if (o == null || o.RepairTableList == null) {
                AlertToast.show("获取任务失败...");
                return;
            }
            SpHelper.setRepairModels(o.RepairTableList);
            repairAdapter.setNewData(o.RepairTableList);
            emptyView.setVisibility(o.RepairTableList.size() == 0 ? View.VISIBLE : View.GONE);
        }));
    }
}
